package com.fhz.kotlinstest.paging

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.fhz.kotlinstest.R

private const val TAG = "MyAdapter"
class MyAdapter(private val items: List<Student>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.item_text)
        val image: ImageView = itemView.findViewById(R.id.item_image)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.my_item_layout, parent, false)
        Log.d(TAG, "onCreateViewHolder: ")
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        Log.d(TAG, "onBindViewHolder: ")
        holder.textView.text = items[position].name

        Glide.with(holder.image.context)
            .load("https://th.bing.com/th/id/R.c6fc977ab1effc2f57c1b61ea0b4a10b?rik=xOkA2BAHTYBnfA&riu=http%3a%2f%2fpic.3gbizhi.com%2f2016%2f0619%2f20160619103137648.jpg&ehk=NulKD8j21Eu6wB9zuAYpgZ8H3J7vxPq8IiYqI%2bx%2fRy8%3d&risl=&pid=ImgRaw&r=0")
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(holder.image)
    }
    override fun getItemCount() = items.size
}